﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MixScript.BLL.FI;
using System.Data;
using System.Collections;
using MixScriptWeb.Share;
using MixScript.BLL;
using System.Drawing;

namespace MixScriptWeb.Accounting
{
    public partial class PopUpCoaP : System.Web.UI.Page
    {
        private static GroupCOA oGCOA;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                string pid = "";
                pid = Request.QueryString["ID"];
                if (Request.QueryString["ID"] != null)
                {
                    if (Request.QueryString["type"] == "A")
                    {
                        if (Request.QueryString["PS"] == "P")
                        {
                            lblTitle.Text = "Chart Of Account Groups(Add New)";
                            PopulateLayout(true, true);
                            trSubApplication.Visible = false;
                            ViewState["mode"] = "GA";

                        }
                        else
                        {
                            lblTitle.Text = "Chart Of Account Sub Groups(Add New)";
                            lblGroup.Text = "Sub Groups Name";
                            PopulateLayout(false, true);
                            ViewState["mode"] = "SGA";
                        }
                    }
                    else
                    {
                        if (Request.QueryString["PS"] == "P")
                        {
                            lblTitle.Text = "Chart Of Account Groups(Edit)";
                            PopulateLayout(true, false);
                            trSubApplication.Visible = false;
                            ViewState["mode"] = "GU";
                        }
                        else
                        {
                            lblTitle.Text = "Chart Of Account Sub Groups(Edit)";
                            lblGroup.Text = "Sub Groups Name";
                            PopulateLayout(false, false);
                            ViewState["mode"] = "SGU";
                        }
                    }
                }
                else
                {
                    if (pid == null)
                    {
                        if (Request.QueryString["PS"] == "P")
                        {
                            lblTitle.Text = "Chart Of Account Groups(Add New)";
                            PopulateLayout(true, true);
                            ViewState["mode"] = "GA";
                        }
                        else
                        {
                            lblTitle.Text = "Chart Of Account Sub Groups(Add New)";
                            PopulateLayout(false, false);
                            ViewState["mode"] = "SGA";
                        }
                    }

                }

            }
        }
        private void PopulateLayout(bool isGroups, bool isAddNew)
        {
            //txtAccSubParentNo.Visible = false;
            int idx;
            if (!isAddNew)
            {
                DataTable dt = new DataTable();
                Int32 idcoa;
                idcoa = Convert.ToInt32(Request.QueryString["ID"]);
                oGCOA = new GroupCOA();

                if (isGroups)
                {

                    txtAccSubParentNo.Visible = false;
                    txtAccParentNo.Text = Request.QueryString["ID"];
                    if (oGCOA.getGroupCOA(ref dt, idcoa))
                    {
                        txtAccName.Text = (string)dt.Rows[0].ItemArray[1];
                        if (dt.Rows[0].ItemArray[2] == null)
                        { ddlApplication.SelectedIndex = 0; }
                        else
                        {
                            int.TryParse(dt.Rows[0].ItemArray[2].ToString(), out idx);
                            ddlApplication.SelectedIndex = (idx - 1);
                        }
                    }
                }
                else
                {
                    txtAccParentNo.Text = Request.QueryString["ID"].Substring(0, 1);
                    txtAccSubParentNo.Text = Request.QueryString["ID"].Substring(1, 2); ;
                    if (oGCOA.getSubGroupCOA(ref dt, idcoa))
                    {
                        txtAccName.Text = (string)dt.Rows[0].ItemArray[2];
                        if (dt.Rows[0].ItemArray[3] == null)
                            ddlApplication.SelectedIndex = 0;
                        else
                        {
                            int.TryParse(dt.Rows[0].ItemArray[3].ToString(), out idx);
                            ddlApplication.SelectedIndex = (idx - 1);
                        }
                        if (dt.Rows[0].ItemArray[4] == null)
                            ddlSubApplication.SelectedIndex = 0;
                        else
                        {
                            int.TryParse(dt.Rows[0].ItemArray[4].ToString(), out idx);
                            ddlSubApplication.SelectedIndex = (idx - 1);
                        }
                    }
                }
            }
            else
            {

                if (isGroups)
                {
                    txtAccSubParentNo.Visible = false;

                }
                else
                {
                    txtAccParentNo.Text = Request.QueryString["ID"].Substring(0, 1);
                }
            }
        }
        protected bool valid(Int32 length)
        {
            if (length == 1)
            {
                if (txtAccParentNo.Text.TrimEnd().Length > length)
                {
                    lblMessage.Text = "Max length for Parent No are " + length.ToString();
                }
            }
            else if (length == 3)
            {
                if (txtAccSubParentNo.Text.TrimEnd().Length != length)
                {
                    lblMessage.Text = "length for Sub Parent No are " + length.ToString();
                }
            }

            return false;
        }
        protected void btnSave_Click(Object sender, EventArgs e)
        {
            GroupCOA gCoa = new GroupCOA();
            string subGroup;
            subGroup = txtAccParentNo.Text + txtAccSubParentNo.Text;
            int idGroup, idSubGroup;
            int.TryParse(txtAccParentNo.Text, out idGroup);
            int.TryParse(subGroup, out idSubGroup);
            if ((string)ViewState["mode"] == "GA")
                if (valid(1))
                    gCoa.saveGroup(idGroup, txtAccName.Text, ddlApplication.SelectedItem.Value, true);
                else
                    return;
            else if ((string)ViewState["mode"] == "GU")
                if (valid(1))
                    gCoa.saveGroup(idGroup, txtAccName.Text, ddlApplication.SelectedItem.Value, false);
                else
                    return;
            else if ((string)ViewState["mode"] == "SGA")
                if (valid(3))
                    gCoa.saveSubGroup(idGroup, idSubGroup, txtAccName.Text, ddlApplication.SelectedItem.Value, ddlSubApplication.SelectedItem.Value, true);
                else
                    return;
            else if ((string)ViewState["mode"] == "SGU")
                if (valid(3))
                    gCoa.saveSubGroup(idGroup, idSubGroup, txtAccName.Text, ddlApplication.SelectedItem.Value, ddlSubApplication.SelectedItem.Value, false);
                else
                    return;

            Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>window.opener.location.reload(true);self.close();</script>");

        }

    }
}